// Ninf-G-XX, ver. 0.1
// Copyright 2011 National Institute of Advanced Industrial Science and Technology
// 
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// 
//     http://www.apache.org/licenses/LICENSE-2.0
// 
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef __NC_ARG_H__
#define __NC_ARG_H__

#include "Types.hpp"
#include "ArgShape.hpp"
#include "DataOutputStream.hpp"
#include "GrpcData.hpp"
namespace ninf {
namespace lib {

class ParamDesc;

class ArgShape;
class Arg{
  char * writeDataRecursive(DataOutputStream & dos, int dimCounter, char * p);
  char * readDataRecursive(DataInputStream & dis, int dimCounter, char * p);
  void writeScalar(DataOutputStream & dos);
  void readScalar(DataInputStream & dos);

public:
  Arg();
  Arg(ninf::data::GrpcData * gdata, void * ptr);
  virtual ~Arg();

  union {
    char   c;
    short  s;
    int    i;
    long   l;
    float  f;
    double d;
    void * p;
  } v;
  ninf::lib::DataType dataType;
  ninf::lib::ModeType modeType;
  int        nDim;
  ninf::lib::ArgShape * shape;
  
  ninf::data::GrpcData * gdata;
  bool                   gdata_allocated_here;

  void writeData(DataOutputStream & dos);
  void readData(DataInputStream & dos);

  void writeToGdata();
  void readFromGdata();

  void setupGdata();
  bool toBeWriteOut();

  void setupShape(ninf::lib::ParamDesc * desc, int * iArgs, int len);

  int  asInt();
  void allocate(bool shrink);

};
}
}
#endif
